/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.sj.parksdk.fragment.profile;

import android.view.View;
import android.widget.TextView;

import com.ruffian.library.widget.RTextView;
import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.fragment.carmanage.FragmentCarManage;
import com.sj.parksdk.fragment.certificate.FragmentUserCertificateContent;
import com.sj.parksdk.fragment.certificate.FragmentUserIdCertificate;
import com.sj.parksdk.fragment.pay.FragmentPayRecord;
import com.sj.parksdk.net.http.MyNetApi;
import com.sj.parksdk.net.http.callback.TipRequestCallBack;
import com.sj.parksdk.net.manager.TokenManager;
import com.sj.parksdk.net.request.MGetRequest;
import com.sj.parksdk.net.result.databean.Data_User;
import com.sj.parksdk.setting.Constans;
import com.sj.parksdk.utils.Utils;
import com.sj.parksdk.utils.XToastUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author xuexiang
 * @since 2019-10-30 00:18
 */
@Page(anim = CoreAnim.none)
public class ProfileFragment extends BaseFragment implements SuperTextView.OnSuperTextViewClickListener {

    @BindView(R2.id.tv_user_name)
    TextView tvUserName;
    @BindView(R2.id.rtv_user_cetificat)
    RTextView rtvUserCetificat;
    @BindView(R2.id.stv_goto_cetificate)
    SuperTextView stvGotoCertificate;
    @BindView(R2.id.menu_car_manage)
    SuperTextView menuCarManage;
    @BindView(R2.id.menu_my_coupons)
    SuperTextView menuMyCoupons;
    @BindView(R2.id.menu_pay_record)
    SuperTextView menuPayRecord;
    @BindView(R2.id.menu_invoice)
    SuperTextView menuInvoice;
    @BindView(R2.id.menu_concat_us)
    SuperTextView menuConcatUs;
    @BindView(R2.id.menu_user_agreement)
    SuperTextView menuUserAgreement;
    @BindView(R2.id.menu_setting)
    SuperTextView menuSetting;

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        return null;
    }

    /**
     * 布局的资源id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_profile;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        initUserInfo();
    }

    public void initUserInfo() {
        if (!TokenManager.getInstance().isUserLogined()){
            logout();
            return;
        }
        requestUserInfo();
    }



    @Override
    protected void initListeners() {
        menuCarManage.setOnSuperTextViewClickListener(this);
        menuConcatUs.setOnSuperTextViewClickListener(this);
        menuInvoice.setOnSuperTextViewClickListener(this);
        menuMyCoupons.setOnSuperTextViewClickListener(this);
        menuPayRecord.setOnSuperTextViewClickListener(this);
        menuSetting.setOnSuperTextViewClickListener(this);
        menuUserAgreement.setOnSuperTextViewClickListener(this);
        stvGotoCertificate.setOnSuperTextViewClickListener(this);
    }


    @SingleClick
    @Override
    public void onClick(SuperTextView view) {
        int id = view.getId();
        if (id == R.id.menu_car_manage) {
            if (checkLogin()) openNewPage(FragmentCarManage.class);
        } else if (id == R.id.menu_concat_us) {
            Utils.goWeb(getContext(), Constans.CONTACT_US);
        } else if (id == R.id.menu_invoice) {
            XToastUtils.warning("开发中");
        } else if (id == R.id.menu_my_coupons) {
            if (checkLogin()) openNewPage(FragmentMyCoupons.class);
        } else if (id == R.id.menu_pay_record) {
            if (checkLogin()) openNewPage(FragmentPayRecord.class);
        } else if (id == R.id.menu_setting) {
            if (checkLogin()) openNewPage(FragmentSetting.class);
        } else if (id == R.id.menu_user_agreement) {
            openNewPage(FragmentAgreement.class);
        } else if (id == R.id.stv_goto_cetificate) {
            openNewPage(FragmentUserIdCertificate.class);
        }
    }

    @SingleClick
    @OnClick({R2.id.tv_user_name, R2.id.rtv_user_cetificat})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.tv_user_name) {
            checkLogin();
        } else if (id == R.id.rtv_user_cetificat) {
            if (TokenManager.getInstance().isUserLogined()
                    && TokenManager.getInstance().getLoginUser() != null
                    && TokenManager.getInstance().getLoginUser().getVerified()) {
                openNewPage(FragmentUserCertificateContent.class);
            } else {
                openNewPage(FragmentUserIdCertificate.class);
            }
        }
    }

    private void requestUserInfo() {
        new MGetRequest(MyNetApi.USER_INFO).user().execute(new TipRequestCallBack<Data_User>() {
            @Override
            public void onSuccess(Data_User user) throws Throwable {
                TokenManager.getInstance().setLoginUser(user);
                updateUserInfo(user);
            }

            @Override
            public void onError(ApiException e) {
                logout();
            }
        });
    }

    private void logout() {
        tvUserName.setText("");
        rtvUserCetificat.setVisibility(View.GONE);
        stvGotoCertificate.setVisibility(View.GONE);
    }

    private void updateUserInfo(Data_User user) {
        rtvUserCetificat.setVisibility(View.VISIBLE);
        tvUserName.setText(user.getUsername());
        rtvUserCetificat.setText(ResUtils.getString(user.getVerified() ? R.string.my_user_cetificat : R.string.my_user_uncetificat));
        rtvUserCetificat.setEnabled(user.getVerified());
        stvGotoCertificate.setVisibility(user.getVerified()?View.GONE:View.VISIBLE);
    }
}
